﻿using System;
using System.Collections.Generic;
using System.Data;
using System.ServiceModel;
using System.Threading;
using Cinch;
using SLS.Base;
using SLS.Document;
using SLS.ExClassLib.Event;
using SLS.ExClassLib.Events;
using SLS.ExClassLib.Message;
using SLS.ExClassLib.RTIService;
using SLS.ExClassLib.WCFClient;
using SLS.WF.Activities;
namespace SLS.Presentation
{
    /// <summary>
    /// HomeDocument show a xaml flow document that comes from the net (or resource if trouble) 
    /// It is supposed to give news and links
    /// </summary>
    [ViewnameToViewLookupKeyMetadata("DocumentCenter", typeof(DocumentCenter))]
    public partial class DocumentCenter: SLBC
    {
        /// <summary>
        /// Constructor
        /// </summary>
        /// 
        public Dictionary<string, object> MessageList;
        public DocumentCenter()
        {
            InitializeComponent();
            Address = "DocumentCenter";
            flagJoinUIFedrate = true;
            flagJoinHLAFedrate = true;
            flagJoinWFFederate = true;

            WorkFlowCenterServiceClient m_WFClient = new WorkFlowCenterServiceClient(new InstanceContext(this), "TcpBinding");
            m_WFClient.Open();
            ClientWFService.Instance.WFClient = m_WFClient;
            _uiSyncContext = SynchronizationContext.Current;
            StartFedrate();
            WFActivity = new WFEditCenterWorkFlow();
            DocumentManagement.Instance.ClientWFManagerAddress = Address;
        } 
       public override void SLBC_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            base.SLBC_Loaded(sender, e); 
        } 
        public override void ProcessUIMessage(MessageEx msg)
        {
            base.ProcessUIMessage(msg);
            try
            {
                string JobID = msg.JobID;
                string doc = "";
                switch (JobID)
                {
                    case "OnStartDocument":
                        doc = (string)msg.Tag;
                        DocumentManagement.Instance.OpenDocument(doc, MainWindowAddress, "TAB_MAIN", "ExTabControl");
                        break;
                    case "OnStartVIPDocument":
                        doc = (string)msg.Tag;
                        Tracer.Verbose(this.Address, "Get " + DateTime.Now.Ticks.ToString() + " " + msg.JobID + " " + msg.MessageID + " on " + msg.TimeStamp.Ticks.ToString());
                        DocumentManagement.Instance.OpenDocument(doc, "MainWindow", "TAB_MAIN", "ExTabControl");
                        break;
                        
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public override void ProcessWFMessage(MessageEx msg)
        {
            try
            {
                _uiSyncContext = SynchronizationContext.Current;
                string JobID = msg.JobID;
                DataRow dwWFSetUp = null;
                string StartQuery = "";
                string WFType = "";
                if (dwWFSetUp != null)
                {
                    StartQuery = dwWFSetUp["StartQuery"].ToString();
                    WFType = dwWFSetUp["WFType"].ToString();
                }
                base.ProcessWFMessage(msg); 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {

        }
    }
}